## Build Figure 2 from Appendix D

install.packages("dplyr")
install.packages("rjson")
install.packages("stringr")
install.packages("ggplot2")

library(dplyr)
library(rjson)
library(stringr)
library(ggplot2)

## Change to your directory that holds measurement_replication
setwd("D://Dropbox//Congressworks//drafts//measurement paper//measurement_replication")

## Parse questioner and congress from hearing ids, fix a typo in the questioner
#  ids from the underlying data, merge with party IDs, coding Delegate Norton
#  as a Democrat.  Then, classify an outcome as potentially biased with the following
#  measure: 1 if they chose a member of their party over a member of the other party,
#  0 if they chose between two members of the same party, and -1 if they chose
#  a member of the other party over a member of their party
party_map <- fromJSON(file = ".//scaling//partymap.json")
comparison_data <- read.csv(".//scaling//comparisons.csv") %>% filter(expert == 1) %>%
  rowwise() %>%
  mutate(questioner1 = unlist(strsplit(id1, "-"))[3], 
         congress1 = substr(unlist(strsplit(id1, "-"))[2], 1, 3),
         questioner2 = unlist(strsplit(id2, "-"))[3],
         congress2 = substr(unlist(strsplit(id2, "-"))[2], 1, 3)) %>%
  mutate(questioner1 = replace(questioner1, questioner1 == "111508", "40903"),
         questioner2 = replace(questioner2, questioner2 == "111508", "40903")) %>%
  rowwise() %>%
  mutate(questioner1_party = ifelse(questioner1 == "70303", "D", ifelse(party_map[[questioner1]][[congress1]] == "100", "D", "R")), 
         questioner2_party = ifelse(questioner2 == "70303", "D", ifelse(party_map[[questioner2]][[congress2]] == "100", "D", "R"))) %>%
  mutate(partisan_bias = ifelse(questioner1_party == rater_party & questioner2_party != rater_party, 1, 
                                ifelse(questioner1_party != rater_party & questioner2_party == rater_party, -1, 0))) %>%
  select(-c(questioner1, congress1, questioner2, congress2))
         
## Regression-based analysis for partisan bias.  There does not appear to be
#  any partisan bias in the rating of informational.  For confrontational,
#  if anything, members appear to be biased against members of their own party
info.fit <- lm(informational ~ partisan_bias, data = comparison_data)
conf.fit <- lm(confrontational ~ partisan_bias, data = comparison_data)
summary(info.fit)
summary(conf.fit)
sd(comparison_data$informational)
sd(comparison_data$confrontational)

## Compare Republican experts to the auditing Democratic expert
audit_data <- read.csv(".//scaling//audit_comparisons.csv") %>% select(id1, id2, informational, confrontational) %>% 
  inner_join(comparison_data %>% filter(rater_party == "R"), by = c("id1", "id2"))

## The auditing Democrat's choices were very similar to the Republcians, and there
#  does not seem to be any systematic difference between R-D pairs versus same party pairs
nrow(audit_data)
info.audit.fit <- lm(I(informational.y - informational.x) ~ partisan_bias, data = audit_data)
conf.audit.fit <- lm(I(confrontational.y - confrontational.x) ~ partisan_bias, data = audit_data)
summary(info.audit.fit)
summary(conf.audit.fit)


# Build Figure 2 from Appendix D
out <- data.frame(rbind(summary(info.fit)$coef[2,1:2], summary(conf.fit)$coef[2,1:2],
                  summary(info.audit.fit)$coef[2,1:2], summary(conf.audit.fit)$coef[2,1:2]))
out$label <- factor(c("Informational", "Confrontational", "Informational (Audit)", "Confrontational (Audit)"), 
                    levels = c("Informational", "Confrontational", "Informational (Audit)", "Confrtontational (Audit)"))
ggplot(data=out, aes(x=label, y = Estimate)) + geom_point() + xlab("Outcome: First Text is More") + ylab("Coefficient of Partisan Bias Towards the First Legislator") +
  geom_errorbar(aes(ymin=Estimate-1.96*Std..Error,ymax=Estimate+1.96*Std..Error), width = .2) + theme(axis.text.x=element_text(size=12),
                                                                                                      axis.title=element_text(size=14))